﻿using Library.Common.Model;
using Library.Core.Domain.Repository;
using Microsoft.AspNetCore.Mvc;
using Synthesize.BaseSet.ServiceInterface.Dtos.Authority.User;
using Synthesize.BaseSet.ServiceInterface.Query.Authority;
using Synthesize.BaseSet.ServiceInterface.UseCase.Authority;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SMS.WebApi.Areas.BaseSet.Controllers
{

    /// <summary>
    /// 用户操作控制器
    /// </summary>
    public class UserController : BaseSetBaseController
    {
        private readonly IUserQueryService _userQueryService;
        private readonly IUserService _userService;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserController(IUserQueryService userQueryService
            , IUserService userService)
        {
            _userQueryService = userQueryService;
            _userService = userService;
        }

        #region 查询

        /// <summary>
        /// 获取用户分页信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseApiResult<PageData<GetUserListOutput>>> GetUserPageList([FromQuery]GetUserListInput input)
        {
            return await _userQueryService.GetUserPageList(input);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseApiResult<GetUserOutput>> GetUserInfo([FromQuery]GetUserInput input)
        {
            return await _userQueryService.GetUserInfo(input);
        }

        /// <summary>
        /// 根据角色获取对应的用户列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseApiResult<List<GetUserOutput>>> GetUserListByRole([FromQuery] GetUserListByRoleInput input)
        {
            return await _userQueryService.GetUserListByRole(input);
        }

        /// <summary>
        /// 根据岗位获取对应的用户列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseApiResult<List<GetUserOutput>>> GetUserListByPost([FromQuery] GetUserListByPostInput input)
        {
            return await _userQueryService.GetUserListByPost(input);
        }

        /// <summary>
        /// 根据部门获取对应的用户列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<BaseApiResult<List<GetUserOutput>>> GetUserListByDepartment([FromQuery] GetUserListByDepartmentInput input)
        {
            return await _userQueryService.GetUserListByDepartment(input);
        }

        #endregion

        #region 更新

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BaseApiResult<bool>> Create(RegisterUserInput input)
        {
            var result = await _userService.Create(input);
            return result;
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<BaseApiResult<bool>> Update(UpdateUserInput input)
        {
            var result = await _userService.Update(input);
            return result;
        }


        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<BaseApiResult<bool>> ResetPassword(ResetUserPasswordInput input)
        {
            var result = await _userService.ResetPassword(input);
            return result;
        }

        /// <summary>
        /// 禁用/启用用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<BaseApiResult<bool>> ForbidOrEnable(ForbidOrEnableInput input)
        {
            var result = await _userService.ForbidOrEnable(input);
            return result;
        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<BaseApiResult<bool>> DeleteById(SingleDeleteInput input)
        {
            var res = await _userService.DeleteById(input);
            return res;
        }
        #endregion
    }
}
